package com.kgc.app.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.app.condition.AppInfoCondition;
import com.kgc.app.dto.AppInfoDto;
import com.kgc.app.mapper.AppCategoryMapper;
import com.kgc.app.mapper.AppInfoMapper;
import com.kgc.app.mapper.AppVersionMapper;
import com.kgc.app.mapper.DataDictionaryMapper;
import com.kgc.app.pojo.*;
import com.kgc.app.service.AppInfoService;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class AppInfoServiceImpl implements AppInfoService {

    @Autowired
    private AppInfoMapper appInfoMapper;

    @Autowired
    private DataDictionaryMapper dataDictionaryMapper;

    @Autowired
    private AppCategoryMapper appCategoryMapper;

    @Autowired
    private AppVersionMapper appVersionMapper;

    @Override
    @Transactional(readOnly = true)
    public PageInfo<AppInfoDto> selectAppInfoByCondition(AppInfoCondition appInfoCondition) {
        //实现步骤

        //1.先查询所有app_info表中的信息
        //1.1创建条件对象
        AppInfoExample appInfoExample = new AppInfoExample();
        AppInfoExample.Criteria criteria = appInfoExample.createCriteria();

        //1.2进行查询条件拼接 如果用户Id等于null 要按照 is null
        if (null == appInfoCondition.getDevId()){
            return null;  //用户不存在，那么不能查询数据
        }else {
            criteria.andDevIdEqualTo(appInfoCondition.getDevId());
        }

        //拼接软件名查询条件
        if (null != appInfoCondition.getSoftwareName() && !appInfoCondition.getSoftwareName().equals("")){
            criteria.andSoftwareNameLike("%"+appInfoCondition.getSoftwareName()+"%");
        }

        //拼接APP状态查询条件
        if (null != appInfoCondition.getStatus() && appInfoCondition.getStatus() != 0){
            criteria.andStatusEqualTo(appInfoCondition.getStatus());
        }

        //拼接所属平台查询条件
        if (null != appInfoCondition.getFlatformId() && appInfoCondition.getFlatformId() != 0){
            criteria.andFlatformIdEqualTo(appInfoCondition.getFlatformId());
        }

        //拼接一级分类查询条件
        if (null != appInfoCondition.getCategoryLevel1() && appInfoCondition.getCategoryLevel1() != 0){
            criteria.andCategoryLevel1EqualTo(appInfoCondition.getCategoryLevel1());
        }

        //拼接二级分类查询条件
        if (null != appInfoCondition.getCategoryLevel2() && appInfoCondition.getCategoryLevel2() != 0){
            criteria.andCategoryLevel2EqualTo(appInfoCondition.getCategoryLevel2());
        }

        //拼接三级分类查询条件
        if (null != appInfoCondition.getCategoryLevel3() && appInfoCondition.getCategoryLevel3() != 0){
            criteria.andCategoryLevel3EqualTo(appInfoCondition.getCategoryLevel3());
        }

        //开启分页
        PageHelper.startPage(appInfoCondition.getPageNo(),appInfoCondition.getPageSize());

        //执行查询
        List<AppInfo> appInfos = appInfoMapper.selectByExample(appInfoExample);

        //把查询到的数据放入到分页插件中
        PageInfo pageInfo = new PageInfo(appInfos);

        //创建一个集合存储信息对象
        List<AppInfoDto> appInfoDtos = new ArrayList<AppInfoDto>();

        if(null == appInfos || appInfos.size() == 0){
            return null;//当查询APPinfo表里面没有数据，后面你的操作也不需要执行
        }

        for (AppInfo appInfo : appInfos) {

            //2.根据app_info查询到的status查询对应的APP状态
            //2.1创建数据字典标的条件对象
            DataDictionaryExample dataExample01 = new DataDictionaryExample();
            DataDictionaryExample.Criteria dataCriteria01 = dataExample01.createCriteria();
            //2.2拼接两个条件
            dataCriteria01.andTypeNameEqualTo("APP状态");
            dataCriteria01.andValueIdEqualTo(appInfo.getStatus());
            //2.3执行查询 里面其实只有一条数据
            List<DataDictionary> dataDictionaries01 = dataDictionaryMapper.selectByExample(dataExample01);

            //3.根据app_info查询到的flatformId查询对应的所属平台
            //3.1创建条件对象
            DataDictionaryExample dataExample02 = new DataDictionaryExample();
            DataDictionaryExample.Criteria dataCriteria02 = dataExample02.createCriteria();
            //3.2拼接条件
            dataCriteria02.andTypeNameEqualTo("所属平台");
            dataCriteria02.andValueIdEqualTo(appInfo.getFlatformId());
            //3.3执行查询
            List<DataDictionary> dataDictionaries02 = dataDictionaryMapper.selectByExample(dataExample02);

            //4.根据app_info查询到的categoryLevel1查询对应的一级分类
            AppCategory category01 = appCategoryMapper.selectByPrimaryKey(appInfo.getCategoryLevel1());

            //5.根据app_info查询到的categoryLevel2查询对应的二级分类
            AppCategory category02 = appCategoryMapper.selectByPrimaryKey(appInfo.getCategoryLevel2());

            //6.根据app_info查询到的categoryLevel3查询对应的三级分类
            AppCategory category03 = appCategoryMapper.selectByPrimaryKey(appInfo.getCategoryLevel3());

            //7.根据app_info查询到的versionId查询对应的版本信息
            AppVersion appVersion = null;
            if (null != appInfo.getVersionId()){
                appVersion = appVersionMapper.selectByPrimaryKey(appInfo.getVersionId());
            }

            //8.把这些数据封装到AppInfoDto中
            AppInfoDto appInfoDto = new AppInfoDto();

            ConvertUtils.register(new DateLocaleConverter(), Date.class);
            BeanUtils.copyProperties(appInfo,appInfoDto);

            //把状态名称、平台名称、分类名称、版本名称赋值到appInfoDto对象里面去
            appInfoDto.setStatusName(dataDictionaries01.get(0).getValueName());
            appInfoDto.setFlatformName(dataDictionaries02.get(0).getValueName());
            appInfoDto.setCategoryLevel1Name(category01.getCategoryName());
            appInfoDto.setCategoryLevel2Name(category02.getCategoryName());
            appInfoDto.setCategoryLevel3Name(category03.getCategoryName());

            if (null != appVersion){
                appInfoDto.setVersionName(appVersion.getVersionNo());
            }

            //把appInfoDto对象添加到集合中
            appInfoDtos.add(appInfoDto);
        }//循环结束

        pageInfo.setList(appInfoDtos);

        return pageInfo;
    }

    @Override
    public PageInfo<AppInfoDto> selectAppInfoByCondition02(AppInfoCondition appInfoCondition) {
        PageHelper.startPage(appInfoCondition.getPageNo(),appInfoCondition.getPageSize());

        List<AppInfoDto> appInfoDtos = appInfoMapper.selectAppInfoByCondition(appInfoCondition);


        return new  PageInfo<AppInfoDto>(appInfoDtos);
    }

    @Override
    public boolean findAppinfoByAPKName(String APKName) {
        //创建条件类
        AppInfoExample appInfoExample = new AppInfoExample();
        AppInfoExample.Criteria criteria = appInfoExample.createCriteria();

        //拼接查询条件
        criteria.andAPKNameEqualTo(APKName);

        //查询数据
        List<AppInfo> appInfos = appInfoMapper.selectByExample(appInfoExample);


        if (null == appInfos || appInfos.size() == 0) {
            return true;//APKName可用
        }
        return false;
    }

    @Override
    public boolean addAppInfo(AppInfo appInfo) {
        int row = appInfoMapper.insertSelective(appInfo);
        return row >0;
    }

    @Override
    public AppInfo viewAppInfo(Long id) {
        AppInfo appInfo = appInfoMapper.selectByPrimaryKey(id);
        return appInfo;
    }

    @Override
    public Integer updateAppInfo(AppInfo appInfo) {
        int row = appInfoMapper.updateByPrimaryKeySelective(appInfo);

        return row;
    }

    @Transactional
    @Override
    public boolean deleteAppInfoById(Long id) {
        //删除 不光要删除app_info中的APP信息，还需要删除app_version版本表中对应的APP的信息
        AppInfo appInfo = appInfoMapper.selectByPrimaryKey(id);
        //删除app_info表中的APP信息
        int row = appInfoMapper.deleteByPrimaryKey(id);

        //判断在版本中报表中有没有对应的版本
        if (null != appInfo.getVersionId()){
            //删除app_version表中的APP信息
            AppVersionExample example = new AppVersionExample();
            AppVersionExample.Criteria criteria = example.createCriteria();
            //拼接条件
            criteria.andAppIdEqualTo(id);
            //执行删除版本的SQL语句
            appVersionMapper.deleteByExample(example);
        }
        return row > 0;
    }

    @Override
    public AppInfoDto selectAppInfoDtoById(Long id) {


        return appInfoMapper.selectAppInfoById(id);
    }
}
